import numpy as np
import pandas as pd
from pyecharts import Bar


class ChipotleNan:
    def __init__(self):
        self.csv_name = 'https://raw.githubusercontent.com/justmarkham/DAT8/master/data/chipotle.tsv'
        self.df = pd.read_csv(self.csv_name, sep ='\t')

    def bar(self, axis0, axis1, name = 'test', table_name = 'test1'):
        bar = Bar(name)
        bar.add('商家A', axis0, axis1, is_label_show=True, is_datazoom_show=True)
        return bar

    def series_to_bar(self, series, name = 'test', table_name = 'test1'):
        bar = Bar(name)
        axis0 = series.index
        axis1 = np.round(series.values.ravel(), 1)
        bar.add('商家A', axis0, axis1, is_label_show=True, is_datazoom_show=True)
        return bar

    def how_many_are_sold_per_item(self):
        df1 = self.df.groupby('item_name').sum()
        df1.drop('order_id', axis = 1, inplace=True)
        return df1

    def bar_how_many_are_sold_per_item(self):
        # chipo = ChipotleNan()
        info = self.how_many_are_sold_per_item()
        # bar = self.bar(info.index, info.quantity)
        bar = self.series_to_bar(info)
        return bar

    def how_much_are_sold_per_item(self):
        ipr = self.df.quantity*(self.df.item_price.str.replace('$','').astype('float64'))
        df1 = self.df.copy()
        df1['ipr'] = ipr
        df1_grounp = df1.groupby('item_name').sum()
        df1_grounp = df1_grounp.drop('order_id', axis=1, inplace=True)
        df1_grounp = df1_grounp.drop('quantity', axis=1, inplace=True)
        va = df1_grounp.values   # 生成一个
        print(va.shape)
        return df1_grounp

    def bar_how_much_are_sold_per_item(self):
        info = self.how_many_are_sold_per_item()
        bar = self.series_to_bar(info, '每种商品销售金额')
        return bar
